package org.example.auth.service;

import lombok.AllArgsConstructor;
import org.example.auth.domain.SecurityUser;
import org.example.auth.domain.UserPO;
import org.example.auth.repository.UserRepository;
import org.example.common.enumerate.ResponseCode;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.util.Objects;

/**
 * 用户管理业务类
 *
 * @author lsliang
 */
@Service
@AllArgsConstructor
public class UserServiceImpl implements UserDetailsService {

    private UserRepository userRepository;


    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        UserPO user = userRepository.findByUsername(username);
        if (Objects.nonNull(user)) {
            return new SecurityUser(user);
        }
        throw new UsernameNotFoundException(ResponseCode.PASSWORD_ERROR.getMsg());
    }

}
